var $image = $('.cropper-box img')
var options = {
    aspectRatio: 1,
    preview: '.preview-img '
}
$image.cropper(options)


$('.choose-img').on('click', function() {
    $('#file').click()
})

$('#file').on('change', function(e) {
    var files = e.target.files
    if (files.length === 0) {
        layer.mes('请选择图片')
        return
    }
    var imgUrl = URL.createObjectURL(files[0])
    $image
        .cropper('destroy')
        .attr('src', imgUrl)
        .cropper(options)


})

$('.upload-img').on('click', function() {
    var base64 = $image
        .cropper('getCroppedCanvas', {
            width: 100,
            height: 100
        })
        .toDataURL('image/png')
    $.ajax({
        url: '/my/update/avatar',
        method: 'POST',
        data: {
            avatar: base64

        },
        success: function(res) {
            console.log(res);
            layer.msg(res.message)
                // if (res.status === 0) {
                //     window.parent.getUserInfo()
                // }
        }
    })

})